﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.Threading;
using Common.Library;
using Common.Utilities;

namespace Social_Celebrities.Views
{
    public partial class BloggerLogin : PhoneApplicationPage
    {
        string UserName = string.Empty;
        string Password = string.Empty;

        private void LoadAds()
        {
            try
            {
                PageHelper.LoadAdControl(LayoutRoot, BlogLoginAd, 2);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadAds Method In BloggerLogin.xaml.cs file file", ex);
                string excepmess = "Exception in LoadAds Method In BloggerLogin.xaml.cs file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
            }
        }
        
        public BloggerLogin()
        {
            InitializeComponent();
            Loaded += BloggerLogin_Loaded;
        }

        void BloggerLogin_Loaded(object sender, RoutedEventArgs e)
        {
            LoadAds();
            if (NavigationService.BackStack.FirstOrDefault().Source.OriginalString.Contains("AddAccount.xaml"))
                NavigationService.RemoveBackEntry();
        }

        private void Signin_Click(object sender, RoutedEventArgs e)
        {            
            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += bg_DoWork;
            bg.RunWorkerCompleted += bg_RunWorkerCompleted;
            UserName=TbUser.Text;
            Password= TbPass.Password;
            bg.RunWorkerAsync();          
        }

        void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {           
            if (Constants.CommentPosted == false)
            {
              NavigationService.GoBack();
            }
            else
            {
             Constants.CommentPosted = false;
             NavigationService.GoBack();
            }
        }

        void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            if (NetworkHelper.IsNetworkAvailableForDownloads())
            {
                PostToblog p = new PostToblog();
                p.Login(UserName, Password);               
            }
            else
            {
                MessageBoxResult result = default(MessageBoxResult);
                 result = MessageBox.Show("No Network Available", "", MessageBoxButton.OK);
                if (result == MessageBoxResult.OK)
                {
                    NavigationService.GoBack();
                }
            }
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            TbUser.Text = string.Empty;
            TbPass.Password = string.Empty;
            NavigationService.GoBack();
        }
    }
}